package getinfo;

import log.WriteToLog;
import org.apache.hadoop.hbase.ClusterStatus;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.client.Admin;
import singleobject.MemoryInfo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class GetRegionServerLoad {
     Timer timer;

    public  GetRegionServerLoad(int seconds){
        timer = new Timer();
        timer.schedule(new RemindTask(),0,seconds*1000);//一秒刷新一次
    }

    class RemindTask extends TimerTask{
        public void run() {
            //System.out.println("task's up");
            String str = new GetRegionServerInfo().sendQuquestByGet("http://192.168.0.41:8080/status/cluster");
            Pattern regex = Pattern.compile("(\\s\\S)*?(hrslave\\d+).*?(requests=\\d+).*?",Pattern.DOTALL);
            Matcher matcher = regex.matcher(str);

            Map<String,Integer> RS = new HashMap<String,Integer>();
            while(matcher.find()){
//                System.out.println(matcher.group(2));      //RSName
//                System.out.println(matcher.group(3).substring(9));    //请求数量
//                Map<String,Integer> RS = new HashMap<String,Integer>();
                RS.put(matcher.group(2),Integer.parseInt(matcher.group(3).substring(9)));
//                MemoryInfo.getREGION().flashQueue(RS);
            }
            MemoryInfo.getREGION().flashQueue(RS);
            //timer.cancel();
        }
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new GetRegionServerLoad(1);//每一秒刷一次
    }

}
